﻿
Partial Class WebForm_ManageVBook
    Inherits My.MyPage

    Protected Sub SearchVBook1_Loaded(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchVBook1.Loaded
        ' Me.SearchVBook1.BindGV()
    End Sub

    Protected Sub QueryVBook1_Loaded(ByVal source As Object, ByVal e As System.EventArgs) Handles QueryVBook1.Loaded
        'Me.QueryVBook1.BindGV()
    End Sub

    Protected Sub SearchVBook1_SelectedIndexChanged(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchVBook1.SelectedIndexChanged
        If Me.TabContainer1.Tabs.Contains(Me.AddTabPanel) = False Then
            Return
        End If

        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.AddVBook1.InitDataForInsert(Me.SearchVBook1.SelectedVolunteerID)
    End Sub

    Protected Sub QueryVBook1_SelectedIndexChanged(ByVal source As Object, ByVal e As System.EventArgs) Handles QueryVBook1.SelectedIndexChanged
        If Me.TabContainer1.Tabs.Contains(Me.AddTabPanel) = False Then
            Return
        End If

        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.AddVBook1.InitDataForEdit(Me.QueryVBook1.DataKey)
    End Sub

    Protected Sub QueryVBook1_Viewing(ByVal source As Object, ByVal e As System.EventArgs) Handles QueryVBook1.Viewing
        If Me.TabContainer1.Tabs.Contains(Me.AddTabPanel) = False Then
            Return
        End If

        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.AddVBook1.InitDataForRead(Me.QueryVBook1.DataKey)
    End Sub

    Protected Sub btnQueryClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles QueryVBook1.btnQueryClick
        Me.QueryVBook1.BindGV()
    End Sub

    Protected Sub AddVBook1_ReIssuing(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVBook1.ReIssuing
        If Me.ReIssueData() = True Then
            Me.TabContainer1.ActiveTab = Me.SearchVBookTab
            Me.SearchVBook1.BindGV()
            Me.QueryVBook1.BindGV()
            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
        End If
    End Sub

    Protected Sub AddVBook1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVBook1.Saving
        If Me.SaveData() = True Then
            Me.TabContainer1.ActiveTab = Me.SearchVBookTab
            Me.SearchVBook1.BindGV()
            Me.QueryVBook1.BindGV()
            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
        End If
    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If

        If Me.CanAdmin = False Then
            Me.AddTabPanel.Visible = False
            Me.SearchVBook1.IsVisible = False
            Me.QueryVBook1.IsVisible = False
        End If


        'If Me.CanAdmin = False Then
        '    Me.UploadPhotoTabPanel.Visible = False
        'End If

    End Sub
End Class

Partial Class WebForm_ManageVBook
    Private Function CheckExist(ByVal CountyID As String, ByVal SerialGroupID As String, ByVal SerialNumberFormat As String) As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("SELECT * FROM VBook ")
        sql.AppendFormat("WHERE 1=1 ")
        sql.AppendFormat("AND CountyID ='{0}' ", CountyID)
        sql.AppendFormat("AND SerialGroupID='{0}' ", SerialGroupID)
        sql.AppendFormat("AND {0} ", SerialNumberFormat)
        MyObj.WriteErrorLog(sql.ToString())
        If dat.GetData(sql).Rows.Count > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    '補發 
    Private Function ReIssueData() As Boolean
        '判斷SerialNumber長度(不可小於四,或大於十)
        If Me.AddVBook1.SerialNumber.Length < 4 OrElse Me.AddVBook1.SerialNumber.Length > 10 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("VBookLengthError"))
            Return False
        End If

        If Me.AddVBook1.Comment.Length > 600 Then
            MyObj.ShowMessage("備註的字數不可大於六百字!")
            Return False
        End If
        If Me.AddVBook1.OldComment.Length > 600 Then
            MyObj.ShowMessage("舊的紀錄冊備註的字數不可大於六百字!")
            Return False
        End If
        'Dim sn As String = ""
        'Dim zeroLoc As Integer = 0
        'Dim numLoc As Integer = 0
        'Dim OneToNin As Char() = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
        'If Me.AddVBook1.SerialNumber.StartsWith("0") Then
        '    zeroLoc = Me.AddVBook1.SerialNumber.IndexOf("0") + 1
        '    numLoc = Me.AddVBook1.SerialNumber.IndexOfAny(OneToNin) + 1
        'End If
        'Dim rightPadNum As Integer = Int32.Parse(IIf(zeroLoc <> 0, Me.AddVBook1.SerialNumber.Length - (numLoc - zeroLoc), Me.AddVBook1.SerialNumber.Length))
        '改固定取六碼
        Dim rightPadNum As Integer = 6
        If Me.CheckExist(Me.AddVBook1.CountyID, Me.AddVBook1.SerialGroupID, "right('0'+cast(SerialNumber as varchar)," & rightPadNum & ") ='" & Me.AddVBook1.SerialNumber.PadLeft(rightPadNum, "0") & "'") Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("SameVBookSerial"))
            Return False
        End If

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            If Me.AddVBook1.FormMode = FormViewMode.Edit Then


                sql.Clear()
                sql.AppendFormat("insert VBook (CountyID,SerialGroupID,SerialNumber,StatePaperID,VolunteerID,IssueDate,CreateDate,CreateBy,Printed,Status,Active,Comment) select ")
                sql.AppendFormat(" '{0}' ", Me.AddVBook1.CountyID)
                sql.AppendFormat(",'{0}' ", Me.AddVBook1.SerialGroupID)
                sql.AppendFormat(",'{0}' ", Me.AddVBook1.SerialNumber)
                sql.AppendFormat(",'{0}' ", Me.AddVBook1.StatePaperID)
                sql.AppendFormat(",'{0}' ", Me.AddVBook1.VolunteerID)
                sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddVBook1.IssueDate)
                sql.AppendFormat(",{0} ", "getdate()")
                sql.AppendFormat(",'{0}' ", Me.LoginID)
                sql.AppendFormat(",'{0}' ", "N")
                sql.AppendFormat(",'{0}' ", "4") '補發
                sql.AppendFormat(",'{0}' ", "Y")
                sql.AppendFormat(",'{0}' ", Me.AddVBook1.Comment)

                dat.AccData(sql)

                sql.Clear()
                sql.AppendFormat("update VBook set")
                sql.AppendFormat(" Active = 'N' ") '停用原志工證
                sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                sql.AppendFormat(",Comment='{0}' ", Me.AddVBook1.OldComment)
                sql.AppendFormat(" where ID='{0}' ", Me.AddVBook1.PrimaryKey)

                dat.AccData(sql)

            End If

            scope.Complete()
        End Using
        Return True

    End Function

    Private Function SaveData() As Boolean

        '判斷SerialNumber長度
        If Me.AddVBook1.SerialNumber.Length < 4 OrElse Me.AddVBook1.SerialNumber.Length > 10 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("VBookLengthError"))
            Return False
        End If
        If Me.AddVBook1.Comment.Length > 600 Then
            MyObj.ShowMessage("備註的字數不可大於六百字!")
            Return False
        End If
        '新發
        If Me.AddVBook1.FormMode = FormViewMode.Insert Then
            If Me.CheckExist(Me.AddVBook1.CountyID, Me.AddVBook1.SerialGroupID, "SerialNumber = '" + Me.AddVBook1.SerialNumber + "'") Then
                MyObj.ShowMessage(Me.GetLocalResourceObject("SameVBookSerial"))
                Return False
            End If

        Else '修改
            'Dim sn As String = ""
            'Dim zeroLoc As Integer = 0
            'Dim numLoc As Integer = 0
            'Dim OneToNin As Char() = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
            'If Me.AddVBook1.SerialNumber.StartsWith("0") Then
            '    zeroLoc = Me.AddVBook1.SerialNumber.IndexOf("0") + 1
            '    numLoc = Me.AddVBook1.SerialNumber.IndexOfAny(OneToNin) + 1
            'End If
            '改固定取六碼
            '  Dim rightPadNum As Integer = Int32.Parse(IIf(zeroLoc <> 0, Me.AddVBook1.SerialNumber.Length - (numLoc - zeroLoc), Me.AddVBook1.SerialNumber.Length))
            Dim rightPadNum As Integer = 6
            If Me.CheckExist(Me.AddVBook1.CountyID, Me.AddVBook1.SerialGroupID, "right('0'+cast(SerialNumber as varchar)," & rightPadNum & ") ='" & Me.AddVBook1.SerialNumber.PadLeft(rightPadNum, "0") & "' ") Then


                If Me.CheckExist(Me.AddVBook1.CountyID, Me.AddVBook1.SerialGroupID, "right('0'+cast(SerialNumber as varchar)," & rightPadNum & ") ='" & Me.AddVBook1.SerialNumber.PadLeft(rightPadNum, "0") & "' AND ID='" & Me.QueryVBook1.DataKey() & "' ") = False Then
                    MyObj.ShowMessage(Me.GetLocalResourceObject("MustSameVBookSerial"))
                    Return False
                End If

            End If
        End If


        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            If Me.AddVBook1.FormMode = FormViewMode.Insert Then
                sql.Clear()
                sql.AppendFormat(" Select * from VBook Where Active='Y' ")
                sql.AppendFormat(" And VolunteerID='{0}' ", Me.AddVBook1.VolunteerID)

                '判斷 Vbook 是否有資料，若無資料再新增
                If dat.GetData(sql).Rows.Count = 0 Then
                    sql.Clear()
                    sql.AppendFormat("insert VBook (CountyID,SerialGroupID,SerialNumber,StatePaperID,VolunteerID,IssueDate,CreateDate,CreateBy,Printed,Status,Active) select ")
                    sql.AppendFormat(" '{0}' ", Me.AddVBook1.CountyID)
                    sql.AppendFormat(",'{0}' ", Me.AddVBook1.SerialGroupID)
                    sql.AppendFormat(",'{0}' ", Me.AddVBook1.SerialNumber)
                    sql.AppendFormat(",'{0}' ", Me.AddVBook1.StatePaperID)
                    sql.AppendFormat(",'{0}' ", Me.AddVBook1.VolunteerID)
                    sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddVBook1.IssueDate)
                    sql.AppendFormat(",{0} ", "getdate()")
                    sql.AppendFormat(",'{0}' ", Me.LoginID)
                    sql.AppendFormat(",'{0}' ", "N")
                    sql.AppendFormat(",'{0}' ", "2") '初次申請
                    sql.AppendFormat(",'{0}'; ", "Y")
                    dat.AccData(sql)

                    sql.Clear()
                    sql.AppendFormat("update Volunteer set")
                    sql.AppendFormat(" Active='Y' ")
                    sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                    sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                    sql.AppendFormat(" where VolunteerID='{0}'; ", Me.AddVBook1.VolunteerID)
                    dat.AccData(sql)
                Else
                    sql.Clear()
                    sql.AppendFormat("update Volunteer set")
                    sql.AppendFormat(" Active='Y' ")
                    sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                    sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                    sql.AppendFormat(" where VolunteerID='{0}'; ", Me.AddVBook1.VolunteerID)
                    dat.AccData(sql)
                    sql.Clear()
                End If


            Else


                sql.AppendFormat("update VBook set")
                sql.AppendFormat(" CountyID='{0}' ", Me.AddVBook1.CountyID)
                sql.AppendFormat(",SerialGroupID='{0}' ", Me.AddVBook1.SerialGroupID)
                sql.AppendFormat(",SerialNumber='{0}' ", Me.AddVBook1.SerialNumber)
                sql.AppendFormat(",StatePaperID='{0}' ", Me.AddVBook1.StatePaperID)
                sql.AppendFormat(",IssueDate='{0:yyyy/MM/dd}' ", Me.AddVBook1.IssueDate)
                sql.AppendFormat(",Comment='{0}' ", Me.AddVBook1.Comment)
                sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                sql.AppendFormat(" where ID='{0}' ", Me.AddVBook1.PrimaryKey)
                dat.AccData(sql)



            End If

            scope.Complete()
        End Using

        Return True
    End Function

    Private ReadOnly Property LoginID() As String
        Get
            Return Context.User.Identity.Name
        End Get
    End Property
End Class
